/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.servicesImpl;

import cz.muni.fi.heathrow.dao.DestinationDAO;
import cz.muni.fi.heathrow.dto.DTOAssembler;
import cz.muni.fi.heathrow.entities.Destination;
import cz.muni.fi.heathrowapi.DestinationService;
import cz.muni.fi.heathrowapi.dto.DestinationDTO;
import cz.muni.fi.heathrowapi.HeathrowRoles;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Jan Richter
 */
@Service
public class DestinationServiceImpl implements DestinationService{

    private DestinationDAO destinationDao;

    public void setDestinationDao(DestinationDAO destinationDao){
        this.destinationDao = destinationDao;
    }
    
    public DestinationServiceImpl() {
    }
    
    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void createDestination(DestinationDTO destination){
        if(destination == null) 
            throw new IllegalArgumentException("destination is null");
        
        Destination dest = new Destination();
        DTOAssembler.UpdateDestinationDomainObject(destination, dest);
        
        destinationDao.createDestination(dest);
        
        DTOAssembler.UpdateDestinationDTO(destination, dest);
    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public DestinationDTO getDestination(Long id) {
       
        return DTOAssembler.CreateDestinationDTO(destinationDao.getDestination(id));
   }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void updateDestination(DestinationDTO destination) {
        if(destination == null) 
            throw new IllegalArgumentException("destination is null");
       
        Destination dest = destinationDao.getDestination(destination.getId());
        DTOAssembler.UpdateDestinationDomainObject(destination, dest);

        destinationDao.updateDestination(dest);

    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void deleteDestination(DestinationDTO destination) {
        if(destination == null) 
            throw new IllegalArgumentException("destination is null");
        
        destinationDao.deleteDestination(destinationDao.getDestination(destination.getId()));
        
    }

    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public List<DestinationDTO> findAll() {
        List<Destination> dest;
        List<DestinationDTO> dtoDest;
   
        dest = destinationDao.findAll();        
        dtoDest = DTOAssembler.createDestinationDTOList(dest);

        return dtoDest;
    }


    @Override
    @Transactional
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public List<DestinationDTO> findDestinationWild(String text, int limit) {
        if(text == null) 
            throw new IllegalArgumentException("text is null");
        List<Destination> dest;
        List<DestinationDTO> dtoDest;
                
        
        dest = destinationDao.findDestinationWild(text, limit);        
        dtoDest = DTOAssembler.createDestinationDTOList(dest);
        
        return dtoDest;
    }
    
}
